package org.btallarm.math;

import java.util.Arrays;

public class Filter {
	
	
	private DelayLine delayLine;
	
	private double poles[];
	
	private static double LP_P_25HZ[] = {
		  9.116412974768e-05,7.243044792051e-05,9.986259391252e-05,0.0001329497139592,
		   0.000172144208398,0.0002178025999792,0.0002702622883392,0.0003296202776443,
		  0.0003958570887786,0.0004688518937408, 0.000548387759559,0.0006337470199881,
		  0.0007244104371659,0.0008193121842821,0.0009173384380203, 0.001017125064592,
		   0.001117099796649, 0.001215494279723,  0.00131032216433, 0.001399490507257,
		   0.001480751640201, 0.001551750721672,  0.00161016074243, 0.001653577372351,
		   0.001679684216883, 0.001686262655776, 0.001671250628833, 0.001632831053728,
		   0.001569449057018, 0.001479936023029, 0.001363479471737,  0.00121973362881,
		   0.001048854861366,0.0008515165321938,0.0006289697535031,0.0003830553144706,
		  0.0001161943229841,-0.000168591600799,-0.0004677343884749,-0.0007770976146655,
		  -0.001092088663625,-0.001407654225676,-0.001718393470385,-0.002018632196211,
		  -0.002302522390538,-0.002564143775246,-0.002797638627165,-0.002997314812143,
		  -0.003157802403056,-0.003274147755686,-0.003341993246786,-0.003357648202967,
		   -0.00331825023708,-0.003221850670048,-0.003067524889262,-0.002855444073858,
		  -0.002586953345711,-0.002264597396889,-0.001892155673035,-0.001474612730948,
		  -0.001018159936566,-0.0005300972710417,-1.878519522924e-05, 0.000506491968888,
		   0.001035649635199, 0.001557975713177,  0.00206230465863,  0.00253723134112,
		    0.00297132279191, 0.003353362510829, 0.003672573512753, 0.003918889919119,
		   0.004083174020343, 0.004157474445509, 0.004135240396491, 0.004011540251184,
		   0.003783244907153, 0.003449196468548, 0.003010339255461,  0.00246981984374,
		   0.001833040053318, 0.001107689674087,0.0003037056163839,-0.0005667816807039,
		  -0.001489570198843,-0.002448538941135,-0.003425852542061,-0.004402201770873,
		  -0.005357081053125,-0.006269105487198,-0.007116351429293,-0.007876718422744,
		  -0.008528321764985,-0.009049878978334,-0.009421114242951,-0.009623150508015,
		  -0.009638895246481,-0.009453405655382,-0.009054224854161, -0.00843169505072,
		  -0.007579218201134,-0.006493479510747,-0.005174620434333,  -0.0036263503009,
		  -0.001856006655434,0.0001254499492023, 0.002303500346864, 0.004660196070535,
		   0.007174352015593, 0.009821789766847,  0.01257565060211,  0.01540674680567,
		     0.0182839655784,   0.0211747125155,   0.0240453852788,  0.02686187423887,
		    0.02959007561977,    0.032196415874,  0.03464837110546,  0.03691497182834,
		     0.0389672977573,  0.04077893147093,   0.0423263833637,   0.0435894683644,
		    0.04455163095832,  0.04520021396537,  0.04552666120225,  0.04552666120225,
		    0.04520021396537,  0.04455163095832,   0.0435894683644,   0.0423263833637,
		    0.04077893147093,   0.0389672977573,  0.03691497182834,  0.03464837110546,
		      0.032196415874,  0.02959007561977,  0.02686187423887,   0.0240453852788,
		     0.0211747125155,   0.0182839655784,  0.01540674680567,  0.01257565060211,
		   0.009821789766847, 0.007174352015593, 0.004660196070535, 0.002303500346864,
		  0.0001254499492023,-0.001856006655434,  -0.0036263503009,-0.005174620434333,
		  -0.006493479510747,-0.007579218201134, -0.00843169505072,-0.009054224854161,
		  -0.009453405655382,-0.009638895246481,-0.009623150508015,-0.009421114242951,
		  -0.009049878978334,-0.008528321764985,-0.007876718422744,-0.007116351429293,
		  -0.006269105487198,-0.005357081053125,-0.004402201770873,-0.003425852542061,
		  -0.002448538941135,-0.001489570198843,-0.0005667816807039,0.0003037056163839,
		   0.001107689674087, 0.001833040053318,  0.00246981984374, 0.003010339255461,
		   0.003449196468548, 0.003783244907153, 0.004011540251184, 0.004135240396491,
		   0.004157474445509, 0.004083174020343, 0.003918889919119, 0.003672573512753,
		   0.003353362510829,  0.00297132279191,  0.00253723134112,  0.00206230465863,
		   0.001557975713177, 0.001035649635199, 0.000506491968888,-1.878519522924e-05,
		  -0.0005300972710417,-0.001018159936566,-0.001474612730948,-0.001892155673035,
		  -0.002264597396889,-0.002586953345711,-0.002855444073858,-0.003067524889262,
		  -0.003221850670048, -0.00331825023708,-0.003357648202967,-0.003341993246786,
		  -0.003274147755686,-0.003157802403056,-0.002997314812143,-0.002797638627165,
		  -0.002564143775246,-0.002302522390538,-0.002018632196211,-0.001718393470385,
		  -0.001407654225676,-0.001092088663625,-0.0007770976146655,-0.0004677343884749,
		  -0.000168591600799,0.0001161943229841,0.0003830553144706,0.0006289697535031,
		  0.0008515165321938, 0.001048854861366,  0.00121973362881, 0.001363479471737,
		   0.001479936023029, 0.001569449057018, 0.001632831053728, 0.001671250628833,
		   0.001686262655776, 0.001679684216883, 0.001653577372351,  0.00161016074243,
		   0.001551750721672, 0.001480751640201, 0.001399490507257,  0.00131032216433,
		   0.001215494279723, 0.001117099796649, 0.001017125064592,0.0009173384380203,
		  0.0008193121842821,0.0007244104371659,0.0006337470199881, 0.000548387759559,
		  0.0004688518937408,0.0003958570887786,0.0003296202776443,0.0002702622883392,
		  0.0002178025999792, 0.000172144208398,0.0001329497139592,9.986259391252e-05,
		  7.243044792051e-05,9.116412974768e-05
		};
	
	
	public Filter() {
		delayLine = new DelayLine(Double.class, 254);
		this.poles = Arrays.copyOf(LP_P_25HZ, LP_P_25HZ.length);
		
	}
	
	public Filter(double[] zeroes, double[] poles) {
		delayLine = new DelayLine(Double.class, poles.length);
		this.poles = Arrays.copyOf(poles, poles.length);
	}

	public double doFilt(double xinput){
		double retVal = 0.0;
		delayLine.push(xinput);		
		for(int i = 0; i < poles.length ; i++){
			retVal += ((Double) delayLine.getElementAt(i)).doubleValue();
		}
		return retVal/poles.length;
	}
	
}
